import { MessageBox } from 'element-ui';

export function messageBox (callbalk, type, message = "") {
  type = type ? type : "warning";
  message = message ? message : "此操作将永久删除这条数据, 是否继续?";

  MessageBox.confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: type
  }).then(() => {
    callbalk && callbalk();
  }).catch(() => {
    console.log("取消删除")
  });
}

// 获取可视区域宽高
export function getViewport () {
  if (window.innerWidth) {
    return {
      w: window.innerWidth,
      h: window.innerHeight
    };
  } else {
    if (document.compatMode === "BackCompat") {
      return {
        w: document.body.clientWidth,
        h: document.body.clientHeight
      };
    } else {
      return {
        w: document.documentElement.clientWidth,
        h: document.documentElement.clientHeight
      };
    }
  }
}

// 生成随机字符串
export function randomString () {
  let str = null;
  var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  // 97~122  Unicode编码
  for (var i = 97; i < 122; i ++) {
    if(arr.length < 32) {
      arr.push(String.fromCharCode(i));
    }
  }
  arr.sort(function () {
    return Math.random() - 0.5;
  })
  str = arr.join('')
  return str;
}
